<?php
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RClassesImporter::cls("dbo.dtos.MemberDTO" );

class UsersModel {
	
	/**
	 * save DT
	 *@param UserDTO $dto
	 */
	function saveUserDTO($dto){
		return SysDatabase::saveDTO($dto);
	}
	
	
	function delete($arrId) {
		if (is_array($arrId)) {
			$arrId = implode(',', $arrId);
		}
		$sQuery		=	"DELETE FROM user 
							WHERE user_id IN ({$arrId}) ";
		return SysDatabase::execute( $sQuery );
	}	
	
	
	function getUserDetails($user_id) {
		$userObj = new MemberDTO();
		$userObj->setKeyValue($user_id);
		SysDatabase::fillData($userObj);
		return $userObj;
	
	}
	

	/**
	 * Toggle the value in fieldName corresponding to the id
	 *
	 * @param string $fiedName
	 * @param int $id
	 */
	function toggleUser($fiedName, $id) {
		if (!$id) {
			return false;
		}
		$sql = "UPDATE user set {$fiedName} = NOT {$fiedName} WHERE user_id = '{$id}' ";
		return SysDatabase::execute($sql);
	}
	
	/**
	 * activate the user
	 *
	 * @param string $activeCode
	 * @param string $userName
	 * @return AccountDTO or false
	 */
	function activeUser($activeCode, $userName, $newPass) {
		$dto = new MemberDTO();
		$dto->resetToNull();
		$dto->activation_code = $activeCode;
		$dto->username = $userName;
		
//		RubikHelper::debugPrePrint($dto);
		if ($dto->checkExisting(array('activation_code', 'username'), true)) {
			$dto->activation_code = "";
			$dto->activate = 1;
			$dto->password = null;
			if ($newPass) {
				$dto->password = $newPass;
			}
			SysDatabase::saveDTO($dto);
			return $dto;
		}
		return false;
	}


	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	/**
	 * Get user profiles
	 * @param $user_id
	 * @return MemberProfilesDTO
	 */
	function getUserProfile($user_id) {
		$userObj = new MemberProfilesDTO();
		$userObj->member_id = $user_id;
		$userObj->checkExisting('member_id', true);
		return $userObj;
	}
	
	function checkExistUser($id){
		$sQuery	= 'SELECT count(*) as soluong FROM user WHERE username="'.$id.'"';		
		return SysDatabase::loadObject($sQuery);	
	}
}
?>